﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using EntityTools.Forms;
using EntityTools.Model;

using WAF.DAO;
using WAF.EntityFramework;

namespace EntityTools
{
    public partial class MainForm : Form
    {
        private string selectDbID;
        private string selectTbID;

        /**************************** Public ****************************/

        public MainForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 当前选中的数据库ID
        /// </summary>
        public string SelectDbID
        {
            get { return selectDbID; }
        }

        /// <summary>
        /// 当前选中的表ID
        /// </summary>
        public string SelectTbID
        {
            get { return selectTbID; }
        }

        /// <summary>
        /// 获取主窗体树控件
        /// </summary>
        /// <returns>主窗体的树控件</returns>
        public TreeView GetTreeView()
        {
            return this.treeViewList;
        }

        /// <summary>
        /// 获取当前的数据库节点
        /// </summary>
        /// <returns>数据库节点</returns>
        public TreeNode GetSelectedDbNode()
        {
            TreeNode selectedNode = treeViewList.SelectedNode;

            if (selectedNode.Level == 0)
               return selectedNode;
            else
               return selectedNode.Parent;
        }



        /**************************** Private  ****************************/

        /// <summary>
        /// 显示数据库列表
        /// </summary>
        private void LoadDatabaseTree()
        {
            treeViewList.Nodes.Clear();

            foreach (DbInfo db in DbInfo.GetDbList())
            {
                TreeNode dbNode = new TreeNode();

                dbNode.Name = db.Id.ToString();
                dbNode.Text = db.Name;
                dbNode.ToolTipText = db.Remark;
                dbNode.ImageIndex = 0;

                treeViewList.Nodes.Add(dbNode);
            }
        }

        /// <summary>
        /// 设置TreeView的图标列表
        /// </summary>
        private void InitTreeViewImageList()
        {
            ImageList imageList = new ImageList();

            imageList.Images.Add(Image.FromFile("../../Resource/Icon/database_1_16.png"));
            imageList.Images.Add(Image.FromFile("../../Resource/Icon/tablewhite.png"));

            treeViewList.ImageList = imageList;
        }


        /**************************** 窗体事件 ****************************/

        private void MainForm_Load(object sender, EventArgs e)
        {
            InitTreeViewImageList();

            LoadDatabaseTree();

            gridFieldInfo.AutoGenerateColumns = false;
        }

        //新增数据库按钮事件
        private void toolStripButtonAddDb_Click(object sender, EventArgs e)
        {
            DbInfos form = new DbInfos(this);
            form.ShowDialog();
        }

        //删除数据库按钮事件
        private void toolStripButtonDeleteDb_Click(object sender, EventArgs e)
        {
            if (treeViewList.SelectedNode == null)
            {
                MessageBox.Show("请选择一个数据库");
                return;
            }
            else
            {
                long dbid = long.Parse(treeViewList.SelectedNode.Name);
                DialogResult result = MessageBox.Show("确定要删除此数据库中所有信息吗？", "确认", MessageBoxButtons.YesNo);

                if (result == System.Windows.Forms.DialogResult.Yes)
                {
                    DbInfo.DeleteDatabaseInfo(dbid);
                    treeViewList.Nodes.Remove(treeViewList.SelectedNode);
                }
            }
        }

        //修改数据库按钮事件
        private void toolStripButtonUpdateDb_Click(object sender, EventArgs e)
        {

        }

        //新增数据库表按钮事件
        private void toolStripButtonAddTable_Click(object sender, EventArgs e)
        {
            TableInfos form = new TableInfos(this);
            form.ShowDialog();
        }

        //删除数据库表按钮事件
        private void toolStripButtonDeleteTable_Click(object sender, EventArgs e)
        {

        }

        //修改数据库表按钮事件
        private void toolStripButtonUpdateTable_Click(object sender, EventArgs e)
        {

        }

        //新增字段按钮事件
        private void btnAddField_Click(object sender, EventArgs e)
        {
            if (treeViewList.SelectedNode == null || treeViewList.SelectedNode.Level != 1)
            {
                MessageBox.Show("请选择一个数据库表");
                return;
            }

            FieldInfos form = new FieldInfos(this);
            form.ShowDialog();
        }

        //删除字段按钮事件
        private void btnDeleteFiled_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("确定要删除这些字段吗？", "确认", MessageBoxButtons.YesNo);

            if (result == System.Windows.Forms.DialogResult.Yes)
            {
                foreach (DataGridViewRow row in gridFieldInfo.SelectedRows)
                {
                    EntityAction.Delete(typeof(FieldInfo), (long)row.Cells["Id"].Value);
                }
            }
        }

        //修改字段按钮事件
        private void btnUpdateField_Click(object sender, EventArgs e)
        {

        }

        //向上移动字段按钮事件
        private void btnMoveUp_Click(object sender, EventArgs e)
        {

        }

        //向下移动字段按钮事件
        private void btnMoveDown_Click(object sender, EventArgs e)
        {

        }

        //单击树节点事件
        private void treeViewList_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node.Level == 0)
            {
                selectDbID = e.Node.Name;
                selectTbID = "";
            }
            else
            {
                selectDbID = e.Node.Parent.Name;
                selectTbID = e.Node.Name;

                gridFieldInfo.DataSource = FieldInfo.GetFieldGrid(selectTbID);

            }
        }

        //双击树节点事件
        private void treeViewList_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            TreeNode selectedNode = e.Node;

            if (selectedNode.Level == 0)
            {
                if (selectedNode.Nodes.Count == 0)
                {
                    foreach (TableInfo tb in TableInfo.GetTableList(selectedNode.Name))
                    {
                        TreeNode node = new TreeNode();

                        node.Name = tb.Id.ToString();
                        node.Text = tb.TableName;
                        node.ToolTipText = tb.Remark;
                        node.ImageIndex = 1;
                        node.SelectedImageIndex = 1;

                        selectedNode.Nodes.Add(node);
                    }

                    selectedNode.Expand();
                }
            }
            else
            { 
                
            }
        }


    }
}
